package com.wk.controller;

import com.wk.dto.ResponseResult;
import com.wk.dto.verificationcode.VerifyCodeResponse;
import com.wk.request.CodeVerifyRequest;
import com.wk.service.VerifyCodeService;
import com.wk.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("verify-code")
@Slf4j
public class VerifyCodeCtrl {

	@Autowired
	private VerifyCodeService verifyCodeService;

	/**
	 * 根据身份，手机号，生成验证码
	 * @param identity
	 * @param phoneNumber
	 * @return
	 */
	@GetMapping("/generate/{identity}/{phoneNumber}")
	public ResponseResult<VerifyCodeResponse> generate(@PathVariable("identity") int identity , @PathVariable ("phoneNumber") String phoneNumber) {
		log.info("身份类型："+identity+",手机号："+phoneNumber);
		return verifyCodeService.generate(identity,phoneNumber);
	}


	@PostMapping("/verify")
	public ResponseResult verify(@RequestBody CodeVerifyRequest request) {
		log.info("/verify-code/verify  request:"+ JsonUtil.obj2String(request));
		//获取手机号和验证码
		String phoneNumber = request.getPhoneNumber();
		int identity = request.getIdentity();
		String code = request.getCode();

		return verifyCodeService.verify(identity,phoneNumber,code);

	}
}
